package com.brillilab.starter.controller.aio.experiments;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.pa.experiments.StepReagentAdd;
import com.brillilab.domain.pa.experiments.StepTextAdd;
import com.brillilab.domain.pa.experiments.StepTimerAdd;
import com.brillilab.domain.vo.experiments.*;
import com.brillilab.domain.vo.method.MethodStepReagentImportVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.logic.experiments.ExperimentStepContentLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ExperimentStepContentController {

    @Resource
    private UsersLogic usersLogic;
    @Resource
    private ExperimentStepContentLogic experimentStepContentLogic;

    /**
     * 实验步骤中添加Kit时获取Kit对应reagent列表
     * @param kitId
     * @param amount
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/api/experiment/step/kit/reagent/list",method=RequestMethod.GET)
    public ResponseVo getKitReagentList(Long kitId,Long groupId,BigDecimal amount,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(kitId!=null && amount!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<ExperimentStepReagentVo> stepReagentVos = experimentStepContentLogic.getKitReagentList(kitId,groupId,amount,userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success(stepReagentVos);
    }

    /**
     * 实验步骤中添加试剂/样品成分
     * @param stepReagentAdd
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/api/experiment/step/reagent/add",method=RequestMethod.POST)
    public ResponseVo addReagentToStep(@RequestBody StepReagentAdd stepReagentAdd,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(stepReagentAdd!=null && StringUtils.isNotBlank(token)
                && stepReagentAdd.getExperimentId()!=null && stepReagentAdd.getPhaseId()!=null
                && stepReagentAdd.getStepId()!=null && StringUtils.isNotBlank(stepReagentAdd.getContent()) ,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentStepContentLogic.addReagentToStep(stepReagentAdd.getExperimentId(),stepReagentAdd.getPhaseId(),stepReagentAdd.getStepId(),
                stepReagentAdd.getContent(),stepReagentAdd.getItems(),userInfo);

        return ResponseVo.success();
    }

    /**
     * 实验步骤中添加试剂/样品成分
     * @param stepReagentAdd
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/api/experiment/step/reagent/addNew",method=RequestMethod.POST)
    public ResponseVo addReagentToStepNew(@RequestBody StepReagentAdd stepReagentAdd,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(stepReagentAdd!=null && StringUtils.isNotBlank(token)
                && stepReagentAdd.getExperimentId()!=null && stepReagentAdd.getPhaseId()!=null
                && stepReagentAdd.getStepId()!=null && StringUtils.isNotBlank(stepReagentAdd.getContent()) ,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentStepContentLogic.addReagentToStepNew(stepReagentAdd.getExperimentId(),stepReagentAdd.getPhaseId(),stepReagentAdd.getStepId(),
                stepReagentAdd.getContent(),stepReagentAdd.getItems(),userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 实验步骤中添加文本
     * @param stepTextAdd
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/api/experiment/step/text/add",method=RequestMethod.POST)
    public ResponseVo addTextToStep(@RequestBody StepTextAdd stepTextAdd,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(stepTextAdd!=null && StringUtils.isNotBlank(token)
                && stepTextAdd.getExperimentId()!=null && StringUtils.isNotBlank(stepTextAdd.getContent()),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        ExperimentStepTextVo textVo=experimentStepContentLogic.addTextToStep(stepTextAdd.getExperimentId(),stepTextAdd.getPhaseId(),stepTextAdd.getStepId(),
                stepTextAdd.getContent(),userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success(textVo);
    }

    /**
     * 实验步骤中计时器添加
     * @param stepTimerAdd
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/api/experiment/step/timer/add",method=RequestMethod.POST)
    public ResponseVo addTimerToStep(@RequestBody StepTimerAdd stepTimerAdd,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(stepTimerAdd!=null && StringUtils.isNotBlank(token) && stepTimerAdd.getExperimentId()!=null
                && (stepTimerAdd.getTime()!=null || !CollectionUtils.isEmpty(stepTimerAdd.getTimeList())),"请求参数缺失！");

//        Assert.isTrue(stepTimerAdd.getPhaseId()!=null && stepTimerAdd.getStepId()!=null,"");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        ExperimentStepTimerVo rs =experimentStepContentLogic.addTimerToStep(stepTimerAdd.getExperimentId(),stepTimerAdd.getPhaseId()
                ,stepTimerAdd.getStepId(),stepTimerAdd.getTime(),stepTimerAdd.getTimeList(),userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success(rs);
    }

    /**
     * 实验步骤列表文本
     * @param experimentId
     * @param phaseId
     * @param stepId
     * @param token
     * @return
     */
    @RequestMapping(value="/api/experiment/step/text/list",method=RequestMethod.GET)
    public ResponseVo stepTextList(Long experimentId,Long phaseId,Long stepId,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(experimentId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<ExperimentStepTextGroupVo> textVos = experimentStepContentLogic.getStepTextList(experimentId,phaseId,stepId,userInfo.getLabId());

        return ResponseVo.success(textVos);
    }

    /**
     * 实验步骤中删除文本
     * @param experimentId
     * @param phaseId
     * @param stepId
     * @param textId
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value = "/api/experiment/step/text/delete", method = RequestMethod.DELETE)
    public ResponseVo textDelete(Long experimentId, Long phaseId, Long stepId, Long textId, @RequestHeader(value = StarterConstant.TOKEN) String token) {

        Assert.isTrue(experimentId != null && textId != null && StringUtils.isNotBlank(token), "请求参数缺失！");

        UserInfoVo userInfo = usersLogic.getUserByToken(token);

        experimentStepContentLogic.textDelete(experimentId,phaseId,stepId,textId,userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 实验步骤列表试剂
     * @param experimentId
     * @param phaseId
     * @param stepId
     * @param token
     * @return
     */
    @RequestMapping(value="/api/experiment/step/kit/list",method=RequestMethod.GET)
    public ResponseVo stepKitReagentList(Long experimentId,Long phaseId,Long stepId,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(experimentId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<ExperimentStepMaterialVo> materialVos = experimentStepContentLogic.getStepKitReagentList(experimentId,phaseId,stepId,userInfo.getLabId());

        return ResponseVo.success(materialVos);
    }

    /**
     * 实验步骤列表全部试剂
     * @param experimentId
     * @param token
     * @return
     */
    @RequestMapping(value="/api/experiment/allKit/list",method=RequestMethod.GET)
    public ResponseVo experimentReagentList(Long experimentId,Long phaseId,Long stepId,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(experimentId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<ExperimentStepMaterialVo> materialVos = experimentStepContentLogic.getExperimentReagentList(experimentId,phaseId,stepId,userInfo.getLabId());

        return ResponseVo.success(materialVos);
    }

    /**
     * 实验步骤列表样品
     * @param experimentId
     * @param phaseId
     * @param stepId
     * @param token
     * @return
     */
    @RequestMapping(value="/api/experiment/step/sample/list",method=RequestMethod.GET)
    public ResponseVo stepSampleReagentList(Long experimentId,Long phaseId,Long stepId,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(experimentId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<ExperimentStepMaterialVo> materialVos = experimentStepContentLogic.getStepSampleReagentList(experimentId,phaseId,stepId,userInfo.getLabId());

        return ResponseVo.success(materialVos);
    }

    /**
     * 实验步骤列表全部样品
     * @param experimentId
     * @param token
     * @return
     */
    @RequestMapping(value="/api/experiment/allSample/list",method=RequestMethod.GET)
    public ResponseVo experimentSampleList(Long experimentId,Long stepId,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(experimentId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<ExperimentStepMaterialVo> materialVos = experimentStepContentLogic.getExperimentSampleList(experimentId,userInfo.getLabId(),stepId);

        return ResponseVo.success(materialVos);
    }

    /**
     * 实验步骤列表计时器
     * @param experimentId
     * @param phaseId
     * @param stepId
     * @param token
     * @return
     */
    @RequestMapping(value="/api/experiment/step/timer/list",method=RequestMethod.GET)
    public ResponseVo stepTimerList(Long experimentId,Long phaseId,Long stepId,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(experimentId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<ExperimentStepTimerVo> timerVos = experimentStepContentLogic.getStepTimerList(experimentId,phaseId,stepId,userInfo.getLabId());

        return ResponseVo.success(timerVos);
    }

    /**
     * 实验步骤详情
     * @param experimentId
     * @param phaseId
     * @param stepId
     * @param token
     * @return
     */
    @RequestMapping(value="/api/experiment/step/detail",method=RequestMethod.GET)
    public ResponseVo stepDetail(Long experimentId,Long phaseId,Long stepId,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(experimentId!=null && phaseId!=null && stepId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        ExperimentStepDetailVo stepDetailVo = experimentStepContentLogic.getStepDetail(experimentId,phaseId,stepId,userInfo.getLabId());

        return ResponseVo.success(stepDetailVo);
    }

    /**
     * 实验步骤试剂可导入列表
     * @param experimentId
     * @param phaseId
     * @param stepId
     * @param token
     * @return
     */
    @RequestMapping(value="/api/experiment/step/relateStepAndReagent",method=RequestMethod.GET)
    public ResponseVo methodStepDetail(Long experimentId,Long phaseId,Long stepId,@RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(experimentId!=null && phaseId!=null && stepId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        MethodStepReagentImportVo step=experimentStepContentLogic.getNewestStepAndExperimentStepReagents(experimentId,phaseId,stepId,userInfo.getLabId(),userInfo.getId(),userInfo.getName());

        return ResponseVo.success(step);
    }

    /**
     * 实验步骤导入试剂同步
     * @param methodStepId
     * @param experimentId
     * @param experimentPhaseId
     * @param experimentStepId
     * @param token
     * @return
     */
    @RequestMapping(value="/api/experiment/step/synchronize",method=RequestMethod.GET)
    public ResponseVo methodStepSynchronize(Long methodStepId,Long experimentId,Long experimentPhaseId,Long experimentStepId,@RequestHeader(value=StarterConstant.TOKEN)String token){
        Assert.isTrue(methodStepId!=null && experimentId!=null && experimentPhaseId!=null && experimentStepId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentStepContentLogic.importMethodRelateReagentToExperimentStepReagents(methodStepId,experimentId,experimentPhaseId,experimentStepId,userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 实验步骤 试剂/样品 别名修改
     * @param modifies
     * @param token
     * @return
     */
    @RequestMapping(value = "/api/experiment/step/reagent/alias/modify", method = RequestMethod.POST)
    public ResponseVo aliasModify(@RequestBody AliasModify modifies, @RequestHeader(value = StarterConstant.TOKEN) String token) {

        Assert.isTrue(modifies!=null && modifies.getExperimentId()!=null && modifies.getPhaseId()!=null
                && !CollectionUtils.isEmpty(modifies.getAliases()) && modifies.getStepId()!=null && StringUtils.isNotBlank(token), "请求参数缺失！");

        UserInfoVo userInfo = usersLogic.getUserByToken(token);

        experimentStepContentLogic.aliasModify(modifies.getExperimentId(),modifies.getPhaseId(),modifies.getStepId(),modifies.getAliases(), userInfo);

        return ResponseVo.success();
    }

    /**
     * 实验步骤中删除试剂/样品
     * @param experimentId
     * @param phaseId
     * @param stepId
     * @param id
     * @param token
     * @return
     */
    @RequestMapping(value = "/api/experiment/step/reagent/delete", method = RequestMethod.DELETE)
    public ResponseVo reagentDelete(Long experimentId, Long phaseId, Long stepId, String uuid, Long id, Integer type, @RequestHeader(value = StarterConstant.TOKEN) String token) {

        Assert.isTrue(experimentId != null && phaseId != null && stepId != null && StringUtils.isNotBlank(token), "请求参数缺失！");

        UserInfoVo userInfo = usersLogic.getUserByToken(token);

        experimentStepContentLogic.reagentDelete(experimentId,phaseId,stepId,id,uuid,type,userInfo);

        return ResponseVo.success();
    }

    /**
     * 计时器模板
     * @return
     */
    @RequestMapping(value="/api/experiment/step/timer/template",method=RequestMethod.GET)
    public ResponseVo timerTemplate(){
        List<String> timers=new ArrayList<>();
        timers.add("00:05:00");
        timers.add("00:15:00");
        timers.add("00:30:00");
        timers.add("00:45:00");
        timers.add("01:00:00");
        timers.add("02:00:00");
        timers.add("04:00:00");
        timers.add("06:00:00");
        timers.add("08:00:00");
        timers.add("12:00:00");
        return ResponseVo.success(timers);
    }

}
